<?php
/**
 * @Author: ant
 * @time:2018-1-23
 * @Effect: 轮播图处理
 */

namespace app\admin\controller;

use think\Controller;  
use app\admin\model\Banner as A;
use app\admin\model\BannerM as B;
use app\common\lib\upload;


class Banner  extends Controller
{
	//展示轮播分类
   public function  index()
   {
   	//banner分类
   	$banner=new A();
   	$bannerData=$banner->getBannerTypeDate();
   	//halt($bannerData);
   	$this->assign('bannerdata',$bannerData);
   	$pid=input('param.pid');
    $dataname='';
    if($pid=="")
     {
            $pid=1;
     }
    $BannerModular=$banner->getBannerModularDate($pid); 
    $BannerIdData=$banner->getBannerTypeIdData($pid); 
    $this->assign('BannerD',$BannerIdData);
    $this->assign('BannerM',$BannerModular); 
    //halt($BannerModular);
   	return $this->fetch();
   }
   /**
    * img
    */
   public function img()
   {

   	return $this->fetch();
   }
   //增加轮播图分类
   public function add_bannertype()
   {
   		if(request()->isPost())
            {
				//halt($_POST);
               $banner=new A();
                $res=$banner->add_banner(input('post.'));
                    $mess=array(
                        'status'=>$res['valid'],
                        'message'=>$res['msg']
                    );
                    return $mess;
            }

   	    return $this->fetch();
   }
   /**
    * 上传图片
    */
   public function localuploadImg(){
   	   //halt($_POST);
    // 获取表单上传文件 例如上传了001.jpg
       $path ="/uploads/";
        $file = request()->file('images');
        //halt($file);
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
        if($info){
        // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
        $path = $path . $info->getSaveName();
        
            // 成功上传后 返回上传信息
        return json(array('state'=>1,'path'=>$path));
        }else{
            // 上传失败返回错误信息
        return json(array('state'=>0,'errmsg'=>'上传失败'));
        }
  }
  public function uploadifyImg(){  
        //halt($_FILES);
         $image = Upload::image($_FILES,$_POST['urlRoute']); 
         //图片样式处理
         //$fops = 'imageView2/1/w/800/h/100';
         $fops="";
        // 捕获异常  
        try{  
            // 返回qiniu上的文件名  
           
        }catch(\Exception $e){  
            return json(['state'=>0,'message'=>$e->getMessage()]);  
        }  
        // 返回给uploadify插件状态  
        if($image){  
            $data = [  
                'state' => 1,  
                'message' => 'OK',  
                'path' => config('qiniu.image_url').'/'.$image.'?'.$fops,  
            ];  
            return json($data);exit;       
        }else{  
            return json(['state'=>0,'message'=>'上传失败']);  
        }  
    }  
  /**
   *添加轮播图
   */
  public function add_bannerM()
  {
  	  if(request()->isPost())
            {
				//halt($_POST);
               $banner=new B();
                $res=$banner->add_bannerM(input('post.'));
                    $mess=array(
                        'status'=>$res['valid'],
                        'message'=>$res['msg']
                    );
                    return $mess;
            }
  }

}